package cp1300.ass1.trafficlight;

import java.util.Scanner;
/**
 * 
 * @author MorbidSaint
 *
 */
/**
 * Class to display the user menu
 */
public class Menu {

    /**
     * User entered choice
     */
    UserChoice oUserChoice = new UserChoice();
    /**
     * Main Menu Choice
     */
    Integer iMainMenuchoice;
    
    /**
     * Method to display the menu.<br>
     * Called many times till exit option chosen by the user.
     */
public void displayMenu(){
        
        System.out.println("===Menu===");
        System.out.println("1. Set Number of horizontal lanes (H-Street)[min 1, max 3]");
        System.out.println("2. Set Number of vertical lanes (V-Street)  [min 1, max 4]");
        System.out.println("3. Set Probability of car entering H-Street [min 0, max 1]");
        System.out.println("4. Set Probability of car entering V-Street)[min 0, max 1]");
        System.out.println("5. Run one Simulation cycle");
        System.out.println("6. Set and Run number of simuation cycles   [min 1, max 10]");
        System.out.println("7. Exit");
            
        iMainMenuchoice  = (Integer)readChoice("Enter your choice>",GridConstants.INTEGER);
        
        Integer tempIntObj;
        Double tempDoubleObj;
        
        switch(iMainMenuchoice.intValue()){
        
        case 1: tempIntObj = (Integer) readChoice("Enter the number of Horizontal Lanes:",GridConstants.INTEGER);
                oUserChoice.setHorizontalLanes(tempIntObj.intValue());
                break;
                
        case 2: tempIntObj = (Integer) readChoice("Enter the number of Vertical Lanes:",GridConstants.INTEGER);
        oUserChoice.setVerticalLanes(tempIntObj.intValue());
        break;
        
        case 3: tempDoubleObj = (Double) readChoice("Enter the Prob of car entering Horizontal Lanes:",GridConstants.DOUBLE);
        oUserChoice.setProbabilityHLane(tempDoubleObj.doubleValue());
        break;
        
        case 4: tempDoubleObj = (Double) readChoice("Enter the number of Horizontal Lanes:",GridConstants.DOUBLE);
        oUserChoice.setPropabilityVLane(tempDoubleObj.doubleValue());
        break;
        
        case 5: oUserChoice.setNumberOfSimulationTimes(1);
                startStimulation();
                break;
        
        case 6: tempIntObj = (Integer) readChoice("Enter the number of simulation cycles:",GridConstants.INTEGER);
                oUserChoice.setNumberOfSimulationTimes(tempIntObj.intValue());
                startStimulation();
                break;
        
        case 7: System.out.println("Thanks for using"); System.exit(1);
        
        default: System.out.println("Wrong choice...");
                 break;
        
        }
        
}

/**
 * Method which invokes the startSimulation method of the Grid Class.
 */
private void startStimulation() {
    // TODO Auto-generated method stub

    //Initilising the grid
    Grid oGrid = new Grid(oUserChoice);
    oGrid.startSimulation();
    
}

/**
 * 
 * @param sMessage The message to be displayed on screen.
 * @param iChoice Choice of whether the user expected entry is INTEGER OR DOUBLE.
 * @return returns the user entered value
 */
public Object readChoice(String sMessage,int iChoice){
    
    System.out.println(sMessage);
    Scanner oScanner = new Scanner(System.in);
    
    Object objChoice = null;
    
    if(iChoice == GridConstants.INTEGER){
        objChoice = oScanner.nextInt();
    }
    
    if(iChoice == GridConstants.DOUBLE){
        objChoice = oScanner.nextDouble();
    }
    
    return objChoice;

}

}